<template>
  <div id="excelImportDefine" style="height:100%" v-loading="loading">
    <v-tableTitle :btnConfig="btnConfig" :title="$t('transmit.importTemSetting')" @newCreate="newCreate" @copy="copy" @del="del"></v-tableTitle>
    <ag-grid
      class="base-grid"
      :hasCheck="true"
      :hasIndex="false"
      :hasEdit="true"
      v-model="gridObj"
      :defaultColDef="defaultColDef"
      :highlightCurrentRow="false"
      :disabled="true"
      :data="rowData"
      :rules="rules"
      :height="height"
      :tmsShowFilter="true"
      :columnConfig="columnConfig"
      :extraColumns="extraColumns"
      :showSummary="false"
      :pagination="true"
      :tableSettingVisible.sync="showTableSetting"
      :page="page"
      :pageSize="pageSize"
      :total="total"
      @selection-change="handleSelectionChange"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      @setdialog="setdialog"
      @filter-changed="filterChanged"
      @cell-dblclick="cellDblclick"
      @grid-ready="agGridReady"
    >
    </ag-grid>
  </div>
</template>

<script>
import baseMixin from "@/mixins/BaseMixin";
import agGridBaseMixin from "@/mixins/AgGridBaseMixin";
import { queryExcelImportDefineByPage, batchDeleteExcelImportDefineByIds, getExcelImportType } from "@/api/transmit/transmit";

export default {
  name: "excelImportDefine",
  mixins: [baseMixin, agGridBaseMixin],
  data() {
    return {
      height: 0,
      columnConfig:{ menu_id: 964, menu_code:"excelImportDefine", menu_list_code:"excelImportDefine"},
      defaultColDef: {}
    };
  },
  computed: {
    btnConfig() {
      return [
        { label: this.$t('common.create'), certigier: this.newStatus||(!this.btnPermNameList.includes('EN02070201')&&!this.btnPermNameList.includes('WM03160201')), invalid: "newCreate" },
        { label: this.$t('common.copy'), certigier: this.copyStatus||(!this.btnPermNameList.includes('EN02070202')&&!this.btnPermNameList.includes('WM03160202')), invalid: "copy" },
        { label: this.$t('common.delete'), certigier: this.delStatus||(!this.btnPermNameList.includes('EN02070203')&&!this.btnPermNameList.includes('WM03160203')), invalid: "del" }
      ];
    }
  },
  mounted() {
    this.getExcelImportType();
    this.height = this.getHeight(window,60);
    this.$store.commit("setSkip", {});
    this.$store.commit("setRouteId", "");
    this.$store.commit("setNewFlag", false);
    this.$store.commit("setRouteData", {});
    this.getTableData(this.page, this.pageSize);
  },
  methods: {
    getExcelImportType() {
      getExcelImportType().then(res => {
        this.excelImportTypes = res;
      });
    },
    newCreate() {
      this.$store.commit("setNewFlag", true);
      this.$router.push({name: "excelImportDefineDrawer"});
    },
    copy() {
      let selectedList = this.gridObj.getSelectedRows();
      if (selectedList.length !== 1 || selectedList === null || selectedList === undefined) {
        this.$message.error(this.$t('msg.msg11'));
        return;
      }

      this.$store.commit("setNewFlag", true);
      // 子页面传值
      let query = {copy: true, obj:selectedList[0]};
      this.$router.push({name: "excelImportDefineDrawer", query: query});
    },
    del() {
      let selectedList = this.gridObj.getSelectedRows();
      if (selectedList.length === 0 || selectedList === null || selectedList === undefined) {
        this.$message.error(this.$t('msg.msg12'));
        return;
      }
      let data = [];
      selectedList.forEach(item => {
        data.push(item.id);
      });
      batchDeleteExcelImportDefineByIds(data).then(res => {
          if (res.data === -1) {
            let text = "要删除Excel导入字段定义信息，请先删除对应导入模板配置信息后重试！!";
            this.$message.error(text);
          } else {
            this.$message.success(this.$t('app.delMes'));
            this.getTableData(this.page, this.pageSize);
          }
        
      });
    },
    agGridReady(params) {
      this.gridObj = params.api;
    },
    setdialog(data) {
      this.$store.commit("setRouteData", data);
      this.$router.push({name: "excelImportDefineDrawer"});
    },
    getTableData(page, pageSize) {
      let data = { pageNum: page, pageSize: pageSize };
      queryExcelImportDefineByPage(data).then(res => {

          this.rowData = res.data.list;
          // 转义-导入类型
          this.rowData.forEach((item, index) => {
            // console.log("列表转义 = %o，对象信息 = %o", index, item);
            let importType = item.importType;
            this.excelImportTypes.forEach((x) => {
              let code = parseInt(x.code);
              // console.log("a = %o, b = %o, c = %o", importType, code, x.description);
              if (importType === code) {
                item.importType = x.description;
              }
            });

            // 是否生效
            if (item.isEnabled === 1) {
              item.isEnabled = this.$t('code.yes');
            } else {
              item.isEnabled = this.$t('code.no');
            }

            // 是否默认模板
            if (item.isDefault === 1) {
              item.isDefault = this.$t('code.yes');
            } else {
              item.isDefault = this.$t('code.no');
            }
          });
          this.page = res.pageNum;
          this.pageSize = res.pageSize;
          this.total = res.data.total;
        
      });
    }
  }
};
</script>

<style scoped>
</style>

